from JTK.jQt.InitQtModule import *
from JTK.jQt.QColorScheme import QColorScheme
import JTK.jQt.MessageWidget.MessageBox as mb
from JTK.jQt.JWidget import JLineEdit


class Window(QMainWindow):
    label = 'Multi Node Set Attrbute'
    ver = 'v0.1'
    scheme = QColorScheme.QColorScheme

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.setObjectName(self.label.replace(" ",""))
        self.resize(300, 150)
        self.setWindowTitle(self.label)

        mainWidget = QWidget()
        mainlayout = QVBoxLayout()

        switch_layout = QHBoxLayout()
        self.selected_button = QRadioButton('选中的')
        self.traversal_button = QRadioButton('遍历所有')
        self.filter_check = QCheckBox('过滤类型')
        self.filter_check.setToolTip('如果在选中模式下勾选过滤，则会执行选中组的子对象！')
        self.type_text = QComboBox()
        type_list = ['mesh', 'aiStandIn', 'light', 'transform', 'aiVolume', 'nurbsCurve', 'joint', 'locator',
                     'aiStandardSurface', 'file', 'aiAmbientOcclusion', 'from selected']
        self.type_text.addItems(type_list)

        # self.type_text.setPlaceholderText('Type')
        switch_layout.addWidget(self.selected_button)
        switch_layout.addWidget(self.traversal_button)
        switch_layout.addWidget(self.filter_check)
        switch_layout.addWidget(self.type_text)

        attr_layout = QHBoxLayout()
        attr_label = QLabel()
        attr_label.setText('Attrbute :')
        self.attr_text = JLineEdit.JLineEdit()
        self.attr_text.setPlaceholderText('primaryVisibility')
        actions = ['aiMatte', 'primaryVisibility', 'aiAov', 'aiCamera', 'castsShadows', 'aiSubdivType',
                   'aiSubdivIterations', 'overrideMatte', 'aiIndirect', 'exposure', 'intensity', 'aiOpaque',
                   'aiSamples', 'samples', 'radius', 'visibility', 'aiFilter', 'ignoreColorSpaceFileRules', 'mode']
        for ac in actions:
            self.attr_text.add_right_pop_action(ac)
        # attr_text.set('.aiAov')
        attr_layout.addWidget(attr_label)
        attr_layout.addWidget(self.attr_text)

        value_layout = QHBoxLayout()
        value_label = QLabel()
        value_label.setText('Value      :')
        self.value_text = QLineEdit()
        self.value_text.setPlaceholderText('0')
        value_layout.addWidget(value_label)
        value_layout.addWidget(self.value_text)

        button = QPushButton()
        button.setText('Run')
        button.setMinimumHeight(40)

        mainlayout.addLayout(self.title_layout(self.label, self.ver))

        mainlayout.addLayout(switch_layout)
        mainlayout.addLayout(attr_layout)
        mainlayout.addLayout(value_layout)
        mainlayout.addWidget(button)
        # self.setLayout(mainlayout)
        mainWidget.setLayout(mainlayout)
        self.setCentralWidget(mainWidget)

        # style
        scheme = self.scheme()
        # scheme.setColor(QColor(80, 150, 0, 255), highlightColor=QColor(250, 250, 250), spread=5.5)
        # scheme.applyStyle(button)
        scheme.setColor(QColor(180, 100, 0, 255), highlightColor=QColor(250, 250, 250), spread=5.5)
        scheme.applyStyle(button)

        # 信号槽
        self.selected_button.toggled.connect(self.selected_button_sin)
        self.traversal_button.toggled.connect(self.traversal_button_sin)
        button.clicked.connect(self.run_button)
        self.filter_check.stateChanged.connect(self.filter_check_sin)
        self.type_text.setEnabled(self.filter_check.isChecked())
        self.selected_button.setChecked(True)

    def selected_button_sin(self):
        if self.selected_button.isChecked():
            self.filter_check.setChecked(False)

    def traversal_button_sin(self):
        if self.traversal_button.isChecked():
            self.filter_check.setChecked(True)

    def filter_check_sin(self):
        # if not self.filter_check.isChecked():
        self.type_text.setEnabled(self.filter_check.isChecked())
        if self.traversal_button.isChecked() and not self.filter_check.isChecked():
            # import maya.cmds as cmds
            info = '当前模式是遍历整个场景，去掉过滤无法操作,强制勾选回来！'
            mb.MessageBox().error(info)
            self.filter_check.setChecked(True)
            # cmds.warning(info)

        # else:
        #     self.type_text.setEnabled(True)

    def run_button(self):
        import MultiNodeSetAttr.MutiNodeSetAttrTool as mnst
        import importlib
        import maya.cmds as cmds

        importlib.reload(mnst)
        attr = self.attr_text.text()
        value = self.value_text.text()
        if attr and value:
            if value.isdigit():
                value = int(value)
            else:
                try:
                    value = float(value)
                except:
                    pass

            if self.selected_button.isChecked():
                # 通过选择
                if self.filter_check.isChecked():
                    mnst.set_sl_node(attr, value, self.type_text.currentText())
                else:
                    mnst.set_sl_node(attr, value)
            else:
                # 遍历整个场景
                if self.filter_check.isChecked():
                    mnst.set_all_node(attr, value, self.type_text.currentText())
                else:
                    cmds.warning('必须要有过滤类型才能执行整个场景遍历操作！')
        else:
            cmds.warning(u'请输入属性名称和属性值！')


    def title_layout(self, title='', ver=''):
            self.title_HLayout = QHBoxLayout()
            self.title_HLayout.setObjectName("title_HLayout")
            self.line_r = QFrame()
            self.line_r.setMaximumSize(QSize(2, 20))
            self.line_r.setStyleSheet("background-color: rgb(255, 0, 0);")
            self.line_r.setFrameShadow(QFrame.Sunken)
            self.line_r.setLineWidth(1)
            self.line_r.setMidLineWidth(0)
            self.line_r.setFrameShape(QFrame.VLine)
            self.line_r.setFrameShadow(QFrame.Sunken)
            self.line_r.setObjectName("line_r")
            self.title_HLayout.addWidget(self.line_r)
            self.line_g = QFrame()
            self.line_g.setMaximumSize(QSize(2, 20))
            self.line_g.setStyleSheet("background-color: rgb(0, 255, 0);")
            self.line_g.setFrameShape(QFrame.VLine)
            self.line_g.setFrameShadow(QFrame.Sunken)
            self.line_g.setObjectName("line_g")
            self.title_HLayout.addWidget(self.line_g)
            self.line_b = QFrame()
            self.line_b.setMaximumSize(QSize(2, 20))
            self.line_b.setStyleSheet("background-color: rgb(0, 85, 255);")
            self.line_b.setLineWidth(1)
            self.line_b.setFrameShape(QFrame.VLine)
            self.line_b.setFrameShadow(QFrame.Sunken)
            self.line_b.setObjectName("line_b")
            self.title_HLayout.addWidget(self.line_b)
            self.titleIconLab = QLabel()
            self.titleIconLab.setText("")
            self.titleIconLab.setObjectName("titleIconLab")
            self.title_HLayout.addWidget(self.titleIconLab)
            self.titleLab = QLabel()
            self.titleLab.setText(title)
            self.titleLab.setMaximumSize(QSize(16777215, 25))
            self.titleLab.setStyleSheet(u"QLabel#titleLab {\n"
                                        u"    font: 10pt \"隶书\";\n"
                                        u"    color: rgb(0, 131, 200); \n"
                                        u"    padding: 3px;\n"
                                        u"}")
            self.titleLab.setFont(QFont(u'隶书'))
            self.titleLab.setObjectName("titleLab")
            self.title_HLayout.addWidget(self.titleLab)
            spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
            self.title_HLayout.addItem(spacerItem)
            self.titleLab_ver = QLabel()
            self.titleLab_ver.setText(ver)
            self.titleLab_ver.setMaximumSize(QSize(16777215, 25))
            self.titleLab_ver.setStyleSheet(u"QLabel {\n"
                                            u"    font: 9pt \"隶书\";\n"
                                            u"    color: rgb(200, 100, 0); \n"
                                            u"    padding: 3px;\n"
                                            u"}")
            self.titleLab_ver.setObjectName("titleLab_ver")
            self.title_HLayout.addWidget(self.titleLab_ver)
            self.title_tb = QToolButton()
            self.title_tb.setObjectName("title_tb")
            self.title_tb.setHidden(True)
            self.title_HLayout.addWidget(self.title_tb)
            return self.title_HLayout

def run():
    import maya.cmds as cmds
    # import pymel.core as pm
    # import jarryLibs.mayakit.getMayaWindow as gmw
    from JTK.jmaya.core.get_main_window import get_main_window
    name = Window.label.replace(" ","")
    if cmds.window(name, q=1, ex=1):
        cmds.deleteUI(name)
    ui = Window(parent=get_main_window())
    ui.show()

    # in mya
    # import MultiNodeSetAttr


if __name__ == '__main__':

    app = QApplication()
    w = Window()
    w.show()

    app.exec_()

